<template>
  <div>
  <el-drawer title="修改菜单" :visible.sync="dialogFormVisible" size="25%">
      <el-form :inline="true" ref="formInline" :model="formInline" :rules="rules" :label-position="labelPosition" style="width: 120%" label-width="105px" class="demo-form-inline" size="small">
        <el-row>
          <el-col :span="24">
        <el-form-item label="上级菜单：" prop="parent">
        <el-select class="main-select-tree" ref="selectTree" clearable v-model="formInline.parent" style="width: 130%;" placeholder="请选择上级菜单">
          <el-option v-for="item in formatData(menuData)" :key="item.parent" :label="item.label" :value="item.value"
                     style="display: none;"/>
          <el-tree class="main-select-el-tree" ref="selecteltree"
                   :data="menuData"
                   node-key="id"
                   highlight-current
                   :props="defaultProps"
                   @node-click="handleNodeClick"
                   :expand-on-click-node="expandOnClickNode"
                   :default-expand-all='false'/>
        </el-select>
      </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
      <el-form-item label="菜单icon：" prop="icon">
        <e-icon-picker :style="{width: '95%'}" v-model="formInline.icon"/>
      </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="菜单名称：" prop="name">
              <el-input v-model="formInline.name" :style="{width: '140%'}"  placeholder="请输入菜单名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
        <el-form-item label="排序：" prop="sort">
          <el-input-number size="medium" :min="0" :max="999" style="width: 70%;" v-model="formInline.sort"></el-input-number>
        </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="路由地址:" prop="path">
              <el-input v-model="formInline.path" :style="{width: '140%'}" placeholder="请输入路由地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="组件地址:" prop="component">
              <el-input v-model="formInline.component" :style="{width: '140%'}" placeholder="请输入组件地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="是否目录:" prop="is_catalog">
              <el-radio-group v-model="formInline.is_catalog">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="是否缓存:" prop="nocache">
              <el-radio-group v-model="formInline.nocache">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="侧边栏显示:" prop="hidden">
              <el-radio-group v-model="formInline.hidden">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

      <el-form-item label="状态：" prop="status">
        <el-switch v-model="formInline.status"></el-switch>
      </el-form-item>
    </el-form>
    <div class="demo-drawer__footer" style="padding-left: 100px;">
      <el-button @click="resetForm('formInline')">取 消</el-button>
      <el-button type="primary" @click="submitEditForm('formInline')">修 改</el-button>
    </div>
  </el-drawer>
  </div>
</template>

<script>
  import { editMenuData } from '@/api/menu'
  import { successMsg } from '@/utils/tools'
  export default {
    name: 'Edit',
    props:{ /*----接收数据----*/
      menuData:{
        type:Array,
        required:true
      }
    },
    data() {
      return {
        dialogFormVisible: false,
        labelPosition:'right',
        expandOnClickNode: true,
        options:[],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        formInline: {
          // parent: '',
          // name: '',
          // owner:'',
          // phone:'',
          // email:'',
          // status:true,
          // sort:0
        },
        rules:{
          name:[
            { required: true, message: '请输入菜单名称', trigger: 'blur' },
          ],
          icon:[
            { required: true, message: '请选择菜单icon', trigger: 'change' },
          ],
          path:[
            { required: true, message: '请输入菜单路径', trigger: 'blur' },
          ],
          component:[
            { required: true, message: '请输入菜单组件', trigger: 'blur' },
          ],
        }
      };
    },
    methods:{
      submitEditForm(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid){
            editMenuData(this.formInline).then(res=>{
              if(res.code===2000) {
                successMsg(res.msg)
                this.$emit('reloadTable')
                this.dialogFormVisible = false
              }
            })
          }else{
            return false;
          }
        });
      },
      resetForm(formName){
        this.$refs[formName].resetFields();
        this.dialogFormVisible = false
      },
      // 四级菜单
      formatData(data){
        let options = [];
        data.forEach((item,key) => {
          options.push({label:item.name,value:item.id});
          if(item.children){
            item.children.forEach((items,keys) => {
              options.push({label:items.name,value:items.id});
              if(items.children){
                items.children.forEach((itemss,keyss) => {
                  options.push({label:itemss.name,value:itemss.id});
                  if(itemss.children){
                    itemss.children.forEach((itemsss,keysss) => {
                      options.push({label:itemsss.name,value:itemsss.id});
                    });
                  }
                });
              }
            });
          }
        });
        return options;
      },
      handleNodeClick(node){
        this.formInline.parent = node.id;
        this.$refs.selectTree.blur();
      }
    }
  }
</script>

<style>
  .main-select-el-tree .el-tree-node .is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .main-select-el-tree .el-tree-node.is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
</style>
